package com.uploadfile.controller;

import com.uploadfile.pojo.bgWallImg;
import com.uploadfile.service.bgWallService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.UUID;

@Controller
@RequestMapping("/bgWall")
public class bgWallController {
    @Autowired
    private bgWallService bgWallService;
    @Autowired
    private bgWallImg bgWallImg;

    @PostMapping("/save")
    public String save(@RequestParam("img") MultipartFile img, HttpServletRequest request) {
        if (!img.isEmpty()) {
            String path = "D:/upload";
            String name = UUID.randomUUID().toString().replaceAll("-", "");
            String ext = FilenameUtils.getExtension(img.getOriginalFilename());
            String realName = name + "." + ext;
            File file = new File(path, name + "." + ext);
            if (file.exists())
                file.mkdirs();
            try {
                img.transferTo(file);
                bgWallImg.setUrl("/upload/" + realName);
                bgWallService.save(bgWallImg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "redirect:/photo/bgWall";
    }

    @GetMapping("/downLoad")
    public ResponseEntity<byte[]> downLoad(@RequestParam String url, HttpServletRequest request) {
        try {
            FileInputStream fileInputStream = new FileInputStream(url);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len = 0;
            byte[] buf = new byte[1024];
            while (true) {
                try {
                    if (!((len = fileInputStream.read(buf)) != -1)) break;
                } catch (IOException e) {
                    e.printStackTrace();
                }
                baos.write(buf, 0, len);
            }
            HttpHeaders header = new HttpHeaders();
            header.setContentType(MediaType.IMAGE_JPEG);
            try {
                header.setContentDispositionFormData("attachment", new String(url.getBytes("UTF-8"), "ISO-8859-1"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return new ResponseEntity<>(baos.toByteArray(), header, HttpStatus.CREATED);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    @GetMapping("/delete")
    private String delete(@RequestParam String url) {
        bgWallService.delete(url);
        String path = url;
        File file = new File(path);
        if (file.exists())
            file.delete();
        return "redirect:/photo/bgWall";
    }
}
